﻿using System;
using System.Web;
using System.Data;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using ThangNM_LIB.DB;
using System.IO;
using System.Configuration;
using ThangNM_LIB.FUNC;
using DataAccessObject;

namespace ThangNM.Admin
{
    public partial class ProtectedConfigUI : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
                if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                {
                    pnlMain.Visible = false;
                    Commons.setAccessDenied(Page, MsgUC1);
                    return;
                }
                if (StringEx.Equals(OtherEx.getAppSettings("isEnableJob", ConstEx.appSetting_State.Default), ConstEx.appSetting_State.Open))
                    chkOnOffJob.Checked = true;
                else
                    chkOnOffJob.Checked = false;
            }
            //lbl1.Text = "Cache [sitemap] được tạo vào lúc: " + CustomerCache.tTimeOutSitemap.ToString(FormatEx.Date_Time.Format_DateTime);
            //lbl2.Text = "Cache [rss] được tạo vào lúc: " + CustomerCache.tTimeOutRss.ToString(FormatEx.Date_Time.Format_DateTime);
            //lbl3.Text = "Cache [chart] được tạo vào lúc: " + CustomerCache.tTimeOutChart.ToString(FormatEx.Date_Time.Format_DateTime);
            lbl1.Text = "strUrlTempMail: " + CustomerCache.strUrlTempMail;
            lbl2.Text = "strUrlLogMail: " + CustomerCache.strUrlLogMail;
            lbl3.Text = "lstCategory_ZipEO: " + CustomerCache.get_lstCategory_ZipEO().Count;
            lbl4.Text = "tblTemp_sContent_Change_DB : " + CustomerCache.tblTemp_sContent_Change_DB;
            if (CommonsCheck.ListObject_OK(CustomerCache.lstEmailWaitSent))
                lbl5.Text = "lstEmailWaitSent: " + CustomerCache.lstEmailWaitSent.Count;
            else
                lbl5.Text = "lstEmailWaitSent: 0";
        }
        ////hàm mã hóa
        //protected void btnEncrypt_Click(object sender, EventArgs e)
        //{
        //    Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        //    ConfigurationSection section = config.GetSection("connectionStrings");
        //    if (!section.SectionInformation.IsProtected)
        //    {
        //        section.SectionInformation.ProtectSection(cmdProvider.SelectedValue);
        //        config.SaveAs(Server.MapPath("~/App_Data/LogFile/protected.txt"));
        //    }
        //}

        ////hàm giải mã
        //protected void btnDecrypt_Click(object sender, EventArgs e)
        //{
        //    Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        //    ConfigurationSection section = config.GetSection("connectionStrings");
        //    if (section.SectionInformation.IsProtected)
        //    {
        //        section.SectionInformation.UnprotectSection();
        //        config.Save();
        //    }
        //}

        protected void btnClearCache_Click(object sender, EventArgs e)
        {
            CustomerCache.ClearCache();
            MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Xóa cache thành công!");
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (!StringEx.Equals(OtherEx.getAppSettings("isEnableJob", ConstEx.appSetting_State.Default), ConstEx.appSetting_State.Open))
                    return;

                TempEO objTemp = (new TempDAO()).SelectItem(Temp_PK_iTempID_C.gSYNC);
                if (objTemp != null)
                {
                    if (!StringEx.Equals(objTemp.sContent, CustomerCache.tblTemp_sContent_Change_DB))
                    {
                        ////Jobs.Job_Sent_Mail_Notification();
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Chart);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Notification);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Sitemap);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Rss);
                        ////ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Sitemap_Rss);
                        //ThreadPool.QueueUserWorkItem(new Jobs().Job_Update_Last_Modify);


                        #region "update change"
                        CustomerCache.tblTemp_sContent_Change_DB = objTemp.sContent;
                        #endregion
                        Jobs a = new Jobs();
                        a.Job_Update_Chart(null);
                        a.Job_Update_Notification(null);
                        //a.Job_Update_Sitemap(null);
                        //a.Job_Update_Rss(null);
                        a.Job_Update_Last_Modify(null);

                        Jobs.Job_Update_Sitemap_Rss();
                    }
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void btnDisableJob_Click(object sender, EventArgs e)
        {
            try
            {
                Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                var setting = config.AppSettings.Settings["isEnableJob"];
                if (setting != null)
                {
                    if (setting.Value.Equals(ConstEx.appSetting_State.Open))
                        setting.Value = ConstEx.appSetting_State.Default;
                    else
                        setting.Value = ConstEx.appSetting_State.Open;
                }
                else
                {
                    config.AppSettings.Settings.Add("isEnableJob", ConstEx.appSetting_State.Open);
                }

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                lbl1.Text = config.AppSettings.Settings["isEnableJob"].Value;
            }
            catch (Exception ex)
            {
                lbl1.Text = ex.ToString();
            }
        }

        protected void btnGetRootPath_Click(object sender, EventArgs e)
        {
            lbl1.Text= Server.MapPath("~/");
        }

        protected void chkOnOffJob_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                var setting = config.AppSettings.Settings["isEnableJob"];
                if (setting != null)
                {
                    if (setting.Value.Equals(ConstEx.appSetting_State.Open))
                        setting.Value = ConstEx.appSetting_State.Default;
                    else
                    {
                        setting.Value = ConstEx.appSetting_State.Open;
                    }
                }
                else
                {
                    config.AppSettings.Settings.Add("isEnableJob", ConstEx.appSetting_State.Open);
                }

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                lbl1.Text = config.AppSettings.Settings["isEnableJob"].Value;
            }
            catch (Exception ex)
            {
                lbl1.Text = ex.ToString();
            }
        }


    }
}